package mock.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import mock.dto.LogTransferDTO;
import mock.utility.Utility;

public class LogTransferDAOImp implements LogTransferDAO{
	
	private Utility util = new Utility();
	
	/**
	 * lay tat ca cac log bang cardNo
	 */
	@Override
	public List<LogTransferDTO> getLogByCardNo(String cardNo) {
		// TODO Auto-generated method stub
		Connection con = util.createConnection();
		if(con != null){
			PreparedStatement ps = null;
			ResultSet rs = null;
			try {
				ps = con.prepareStatement("select * from Log where CardNo = ?");
				ps.setString(1, cardNo);
				rs = ps.executeQuery();
				List<LogTransferDTO> list = new ArrayList<LogTransferDTO>();
				while(rs.next()){
					int logID = rs.getInt("LogID");
				    int logTypeID = rs.getInt("LogTypeID");
				    int atmID = rs.getInt("ATMID");
				    String cardno = rs.getString("CardNo");
				    Date logDate = rs.getDate("LogDate");
				    double amount;
				    try{
				    	amount = rs.getDouble("Amount");
				    }catch(Exception e){
				    	amount = -1;
				    }
				    String details;
				    try{
				    	details = rs.getString("Details");
				    }catch(Exception e){
				    	details = "";
				    }
				    int toAccountID;
				    try{
				    	toAccountID = rs.getInt("ToAccountID");
				    }catch(Exception e){
				    	toAccountID = -1;
				    }
				    LogTransferDTO logTransfer = new LogTransferDTO(logID, logTypeID, atmID, cardno, logDate, amount, details, toAccountID);
				    list.add(logTransfer);
				}
				if(!list.isEmpty()) return list;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				System.out.println("Khong thuc hien duoc ps!");
			}finally{
				if(con != null)
					try {
						con.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				if(ps != null)
					try {
						ps.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				if(rs != null)
					try {
						rs.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			}
		}
		return null;
	}

	/**
	 * log
	 */
	@Override
	public boolean logging(int logTypeID, int atmID, String cardNo,
			Date logDate, double transferValue, String details, int toAccountID) {
		// TODO Auto-generated method stub
	        Connection con = null;
	        PreparedStatement pst = null;
	        try {
	            con = util.createConnection();
	            String query = "Insert Into [Log] (LogTypeID,ATMID,CardNo,"
	                    + "LogDate,Amount,Details,ToAccountID) Values (?,?,?,?,?,?,?)";
	            pst = con.prepareStatement(query);
	            pst.setInt(1, logTypeID);
	            pst.setInt(2, atmID);
	            pst.setString(3, cardNo);
	            pst.setDate(4, logDate);
	            if(transferValue == -1){
	            	pst.setString(5, null);
	            }else{
	            	pst.setDouble(5, transferValue);
	            }
	            pst.setString(6, details);
	            if(toAccountID == -1){
	            	pst.setString(7, null);
	            }else{
	            	pst.setInt(7, toAccountID);
	            }
	            int result = pst.executeUpdate();
	            if (result > 0) {
	                return true;
	            }
	        } catch (SQLException e) {
	            e.printStackTrace();
	        } catch (Exception e) {
	            e.printStackTrace();
	        } finally {
	            try {
	                if (pst != null) {
	                    pst.close();
	                }
	                if (con != null) {
	                    con.close();
	                }
	            } catch (SQLException e) {
	                e.printStackTrace();
	            } catch (Exception e) {
	                e.printStackTrace();
	            }
	        }
		return false;
	}

 
	/**
	 * get log from startDate to current date
	 */
	@Override
	public List<LogTransferDTO> getLogFromInputDateToNowByCardNo(String cardNo,
			Date startDate) {
		// TODO Auto-generated method stub
		Connection con = util.createConnection();
		if(con != null){
			PreparedStatement ps = null;
			ResultSet rs = null;
			try {
				ps = con.prepareStatement("select * from Log where LogDate >= ? and LogDate <= ? and CardNo = ?");
				ps.setDate(1, startDate);
				ps.setDate(2, util.getCurrentDate());
				ps.setString(3, cardNo);
				rs = ps.executeQuery();
				List<LogTransferDTO> list = new ArrayList<>();
				while(rs.next()){
					int logID = rs.getInt("LogID");
				    int logTypeID = rs.getInt("LogTypeID");
				    int atmID = rs.getInt("ATMID");
				    String cardno = rs.getString("CardNo");
				    Date logDate = rs.getDate("LogDate");
				    double amount;
				    try{
				    	amount = rs.getDouble("Amount");
				    }catch(Exception e){
				    	amount = -1;
				    }
				    String details;
				    try{
				    	details = rs.getString("Details");
				    }catch(Exception e){
				    	details = "";
				    }
				    int toAccountID;
				    try{
				    	toAccountID = rs.getInt("ToAccountID");
				    }catch(Exception e){
				    	toAccountID = -1;
				    }
				    LogTransferDTO logTransfer = new LogTransferDTO(logID, logTypeID, atmID, cardno, logDate, amount, details, toAccountID);
				    list.add(logTransfer);
				}
				if(!list.isEmpty()) return list;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				System.out.println("Khong thuc hien duoc cau query!");
			}finally{
				if(con != null)
					try {
						con.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				if(ps != null)
					try {
						ps.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				if(rs != null)
					try {
						rs.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			}
		}
		return null;
	}
	
}
